package com.learn.project2.controller.admin;

import com.learn.project2.model.bo.admin.coupon.CouponCreateBO;
import com.learn.project2.model.bo.admin.coupon.CouponListBO;
import com.learn.project2.model.bo.admin.coupon.CouponUpdateBO;
import com.learn.project2.model.vo.admin.coupon.*;
import com.learn.project2.service.admin.coupon.CouponService;
import com.learn.project2.utils.Result;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.learn.project2.model.bo.admin.coupon.CouponDeleteBO;
import com.learn.project2.utils.BaseParam;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.regex.Pattern;

/**
 * @description:
 * @author 岳子昂 zhizhuke
 * @date 2022-01-06 22:12
 * @version 1.0
 */
@RequestMapping("admin/coupon")
@RestController
public class CouponController {

    @Autowired
    CouponService couponService;

    /**
     * @description:
     * @param:
     * @return:
     * @author zhizhuke
     * @date: 2022-01-07 23:43
     */
    @GetMapping("list")
    @RequiresPermissions(value = {"admin:coupon:list"})
    public Result list(BaseParam param,String name,Integer type,Integer status){
        CouponListVO couponListVO =  couponService.list(param,name,type,status);
        return Result.ok(couponListVO);
    }

//    /**
//     * @description: 47
//     * @param:
//     * @return:
//     * @author 岳子昂
//     * @date: 2022-01-06 22:13
//     */
//    @RequestMapping("list")
//    public Result list(CouponListBO couponListBO){
//        CouponListVO couponListVO = couponService.list(couponListBO);
//        return Result.ok(couponListVO);
//    }

    /**
     * @description: 48
     * @param:
     * @return:
     * @author 岳子昂
     * @date: 2022-01-06 22:13
     */
    @RequestMapping("create")
    @RequiresPermissions(value = {"admin:coupon:create"})
    public Result create(@RequestBody CouponCreateBO couponCreateBO){
        // 对需要参数进行校验
        // 校验指定参数是否为空，是否为数字
        String total = couponCreateBO.getTotal();
        if (!total.matches("^\\d+$")){
            return Result.fail("优惠券数量必须为正整数");
        }
        String min = couponCreateBO.getMin();
        if (!min.matches("^\\d+$")){
            return Result.fail("最低消费必须为正整数");
        }
        String discount = couponCreateBO.getDiscount();
        if (!discount.matches("^\\d+$")){
            return Result.fail("减满金额必须为正整数");
        }
        String limit = couponCreateBO.getLimit();
        if (!limit.matches("^\\d+$")){
            return Result.fail("每人限领必须为正整数");
        }

        int minInt = Integer.parseInt(min);
        int discountInt = Integer.parseInt(discount);
        if (minInt <= discountInt){
            return Result.fail("最低消费必须大于满减金额");
        }

        // 校验有效期
        Short timeType = couponCreateBO.getTimeType();
        String days = couponCreateBO.getDays();
        Date startTime = couponCreateBO.getStartTime();
        Date endTime = couponCreateBO.getEndTime();
        if (timeType == 0){
            if (!days.matches("^\\d+$")){
                return Result.fail("有效期类型为\"领券相对天数\"时，有效期天数必须为整数");
            }
        }else {
            if (startTime == null||endTime == null){
                return Result.fail("有效期类型为\"指定绝对时间\"时，开始时间与结束时间不能为空");
            }
        }
        CouponCreateVO couponCreateVO = couponService.create(couponCreateBO);
        return Result.ok(couponCreateVO);
    }

    /**
     * @description: 49
     * @param:
     * @return:
     * @author 岳子昂
     * @date: 2022-01-06 22:13
     */
    @RequestMapping("update")
    @RequiresPermissions(value = {"admin:coupon:update"})
    public Result update(@RequestBody CouponUpdateBO couponUpdateBO){

        // 对需要参数进行校验
        // 校验指定参数是否为数字
        String total = couponUpdateBO.getTotal();
        if (!total.matches("^\\d+$")){
            return Result.fail("优惠券数量必须为正整数");
        }
        String min = couponUpdateBO.getMin();
        if (!min.matches("^\\d+$")){
            return Result.fail("最低消费必须为正整数");
        }
        String discount = couponUpdateBO.getDiscount();
        if (!discount.matches("^\\d+$")){
            return Result.fail("减满金额必须为正整数");
        }
        String limit = couponUpdateBO.getLimit();
        if (!limit.matches("^\\d+$")){
            return Result.fail("每人限领必须为正整数");
        }

        int minInt = Integer.parseInt(min);
        int discountInt = Integer.parseInt(discount);
        if (minInt <= discountInt){
            return Result.fail("最低消费必须大于满减金额");
        }

        // 校验有效期
        Short timeType = couponUpdateBO.getTimeType();
        String days = couponUpdateBO.getDays();
        Date startTime = couponUpdateBO.getStartTime();
        Date endTime = couponUpdateBO.getEndTime();
        if (timeType == 0){
            if (!days.matches("^\\d+$")){
                return Result.fail("有效期类型为\"领券相对天数\"时，有效期天数必须为整数");
            }
        }else {
            if (startTime == null||endTime == null){
                return Result.fail("有效期类型为\"指定绝对时间\"时，开始时间与结束时间不能为空");
            }
        }

        // 参数校验结束，执行方法
        CouponUpdateVO couponUpdateVO = couponService.update(couponUpdateBO);
        return Result.ok(couponUpdateVO);
    }

    /**
     * @description: 一个表格上没有的，显示优惠券详情的
     * @param:
     * @return:
     * @author 岳子昂
     * @date: 2022-01-06 22:13
     */
    @RequestMapping("read")
    @RequiresPermissions(value = {"admin:coupon:read"})
    public Result read(Integer id){
        CouponReadVO couponReadVO = couponService.read(id);
        return Result.ok(couponReadVO);
    }


    /**
     * @description: 优惠券管理，删除优惠券http://182.92.235.201:8083/admin/coupon/delete
     * @param:
     * @return:
     * @author zhizhuke
     * @date: 2022-01-06 21:50
     */
    @PostMapping("delete")
    @RequiresPermissions(value = {"admin:coupon:delete"})
    public Result delete(@RequestBody CouponDeleteBO couponDeleteBO) {
        couponService.delete(couponDeleteBO);
        return Result.ok();
    }

    /**
     * @description: 优惠券管理，查询该优惠券专题下的用户http://182.92.235.201:8083/admin/coupon/listuser
     * @author zhizhuke
     * @date 2022-01-07 15-26
     * @version 1.0
     */
    @GetMapping("listuser")
    @RequiresPermissions(value = {"admin:coupon:listuser"})
    public Result listuser(BaseParam param, Integer couponId) {
        CouponListUserVO couponListUserVO =  couponService.listuser(param,couponId);
        return Result.ok(couponListUserVO);
    }
}
